'use strict';

// sass compile
var gulp = require('gulp');
var sass = require('gulp-sass');
var prettify = require('gulp-prettify');
var minifyCss = require("gulp-minify-css");
var rename = require("gulp-rename");
var uglify = require("gulp-uglify");
var rtlcss = require("gulp-rtlcss");
var connect = require('gulp-connect');

//*** Localhost server task
gulp.task('localhost', function() {
  connect.server();
});

gulp.task('localhost-live', function() {
  connect.server({
    livereload: true
  });
});
	

//*** SASS compiler task
gulp.task('sass', function () {
  // bootstrap compilation
    //将指定位置的scss编译为css，存在指定的路径下。**/*就是说下面可以有更下层的文件夹
     gulp.src('./src/sass/global/global.scss').pipe(sass()).pipe(gulp.dest('./dist/css/global/'));
     gulp.src('./src/sass/layouts/**/*.scss').pipe(sass()).pipe(gulp.dest('./dist/css/layouts/'));
     gulp.src('./src/sass/pages/**/*.scss').pipe(sass()).pipe(gulp.dest('./dist/css/pages/'));
     gulp.src('./src/sass/plugins/**/*.scss').pipe(sass()).pipe(gulp.dest('./dist/css/plugins/'));
     gulp.src('./src/sass/components/**/*.scss').pipe(sass()).pipe(gulp.dest('./dist/css/components/'));
   });

//*** SASS watch(realtime) compiler task
gulp.task('watch', function () {
    //监听文件，如果task的第一个参数改变，那么使用该命令的时候，gulp之后的值就改变为对应的。
	gulp.watch('./src/sass/**/*.scss', ['sass']);
	gulp.watch('./src/javascripts/**/*.js', ['copy']);

});

//*** CSS & JS minify task  压缩
gulp.task('minify', function () {
    // css minify
    gulp.src(['./dist/css/global/*.css', '!./dist/css/global/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./dist/css/global/'));
    //将css文件压缩到指定位置且重命名，其中！后面的意思是，已经压缩的不在压缩且重命名为指定格式了。
    gulp.src(['./dist/css/pages/**/*.css', '!./dist/css/pages/**/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./dist/css/pages/'));
    gulp.src(['./dist/css/layout/**/*.css', '!./dist/css/layout/**/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./dist/css/layout/'));
    gulp.src(['./dist/css/components/**/*.css', '!./dist/css/components/**/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./dist/css/components/'));
    gulp.src(['./dist/css/plugins/**/*.css', '!./dist/css/components/**/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./dist/css/components/'));
    //gulp.src(['./bootstrap/layouts/**/scripts/*.js','!./bootstrap/layouts/**/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./bootstrap/layouts/'));
});

// //*** HTML formatter task
// gulp.task('prettify', function() {
//
//   	gulp.src('./**/*.html').
//   	  	pipe(prettify({
//     		indent_size: 4,
//     		indent_inner_html: true,
//     		unformatted: ['pre', 'code']
//    		})).
//    		pipe(gulp.dest('./'));
// });
gulp.task('copy',  function() {
    //复制文件，使用的时候就直接gulp加上前面的类似参数的东西，就可以执行该命令了。
    gulp.src('./src/javascripts/**/*').pipe(gulp.dest('./dist/javascripts/'))
    gulp.src('./src/images/**/*').pipe(gulp.dest('./dist/images/'))
    gulp.src('./bower_components/animate.css/*.css').pipe(gulp.dest('./dist/plugins/animate.css/'))
    gulp.src('./bower_components/jquery/dist/*.js').pipe(gulp.dest('./dist/javascripts/'))
    gulp.src('./bower_components/modernizr/*.js').pipe(gulp.dest('./dist/javascripts/'))
    gulp.src('./bower_components/select2/dist/**/*').pipe(gulp.dest('./dist/plugins/select2/'))
    gulp.src('./bower_components/select2/dist/**/*').pipe(gulp.dest('./dist/plugins/select2/'))

});
